CollectSourceFiles(${CMAKE_CURRENT_SOURCE_DIR} PRIVATE_SOURCES)

if(MSVC)
    # NVDECODE
    set(CUVID_LIB ${CMAKE_CURRENT_SOURCE_DIR}/../../deps/NVDecode/Lib/x64/nvcuvid.lib)
    set(NVENCODEAPI_LIB ${CMAKE_CURRENT_SOURCE_DIR}/../../deps/NVDecode/Lib/x64/nvencodeapi.lib)

    # FFMPEG
    set(NV_FFMPEG_HDRS ${FFMPEG_INCLUDE_DIRS})
    set(NV_FFMPEG_LIBS ${FFMPEG_LIBRARIES})

    # OPENCV-CPU
    # set(OpenCV_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../../deps/Opencv/include/opencv2 ${CMAKE_CURRENT_SOURCE_DIR}/../../deps/Opencv/include)
    # if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    #     set(OpenCV_LIBS ${CMAKE_CURRENT_SOURCE_DIR}/../../deps/Opencv/lib/opencv_world454d.lib)
    # elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
    #     set(OpenCV_LIBS ${CMAKE_CURRENT_SOURCE_DIR}/../../deps/Opencv/lib/opencv_world454.lib)
    # else()
    #     set(OpenCV_LIBS ${CMAKE_CURRENT_SOURCE_DIR}/../../deps/Opencv/lib/opencv_world454.lib)
    # endif()

    set(OpenCV_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../../deps/OpencvGpu/include/opencv2 ${CMAKE_CURRENT_SOURCE_DIR}/../../deps/OpencvGpu/include)
    set(OpenCV_LIBS ${CMAKE_CURRENT_SOURCE_DIR}/../../deps/OpencvGpu/vc16/lib/opencv_world454.lib)
endif()

# CUDA
find_package(CUDA)
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
    set(CMAKE_CUDA_ARCHITECTURES 70 75 80)
endif()
set(CUDA_HOST_COMPILER ${CMAKE_CXX_COMPILER})
SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-gencode arch=compute_50,code=\"sm_50,compute_50\")
if ( CMAKE_COMPILER_IS_GNUCC )
    if(NOT "${CUDA_NVCC_FLAGS}" MATCHES "-std=c\\+\\+11" )
        list(APPEND CUDA_NVCC_FLAGS -std=c++11)
    endif()
endif()
enable_language(CUDA)
list(APPEND PRIVATE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/NVDecode/Utils/ColorSpace.cu")
list(APPEND PRIVATE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/NVDecode/Utils/Resize.cu")

cuda_add_library(decode STATIC ${PRIVATE_SOURCES})
set_target_properties(decode PROPERTIES CUDA_SEPARABLE_COMPILATION ON)

target_include_directories(
    decode SYSTEM BEFORE PUBLIC
    ${NV_FFMPEG_HDRS}
    ${CUDA_INCLUDE_DIRS}
    ${OpenCV_INCLUDE_DIRS}
)

target_link_libraries(
    decode
    Poco::Foundation
    Poco::JSON
    utils
    ${CUDA_LIBRARIES}
    ${CUDA_CUDA_LIBRARY}
    ${CMAKE_DL_LIBS}
    ${NVENCODEAPI_LIB}
    ${CUVID_LIB}
    ${NV_FFMPEG_LIBS}
    ${OpenCV_LIBS}
)